
package ch.fhnw.qualiscan.application.controllers;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.elements.WeighDomain;
import ch.fhnw.qualiscan.application.models.DomainModel;
import ch.fhnw.qualiscan.application.models.QuestionnaireModel;
import ch.fhnw.qualiscan.application.models.RoleModel;
import ch.fhnw.qualiscan.application.models.SurveyModel;
import ch.fhnw.qualiscan.application.models.UserModel;
import ch.fhnw.qualiscan.application.views.NewSurveyView;
import ch.fhnw.qualiscan.core.Controller;
import ch.fhnw.qualiscan.core.db.helpers.addUserToSurveyArgs;
import ch.fhnw.qualiscan.core.db.helpers.questionWeightArgs;
import ch.fhnw.qualiscan.core.db.pojos.Domain;
import ch.fhnw.qualiscan.core.db.pojos.Questionnaire;
import ch.fhnw.qualiscan.core.db.pojos.Role;
import ch.fhnw.qualiscan.core.db.pojos.Survey;
import ch.fhnw.qualiscan.core.db.pojos.SurveyUserRole;
import ch.fhnw.qualiscan.core.db.pojos.User;
import ch.fhnw.qualiscan.core.helpers.EmailHelper;
import ch.fhnw.qualiscan.core.helpers.Permissions;
import com.vaadin.ui.Button;
import com.vaadin.ui.Panel;
import com.vaadin.ui.RichTextArea;
import eu.livotov.tpt.i18n.TM;
import java.util.List;

/**
 *
 * @author bengro
 */
public class NewSurvey extends Controller {
    
    public NewSurveyView newSurveyView;
    
    //POJOS
    public Survey survey;
    public Questionnaire selectedQuestionnaire;
    
    //DB
    public SurveyModel surveyModel;
    public DomainModel domainModel;
    public UserModel userModel;
    public RoleModel roleModel;
    public QuestionnaireModel questionnaireModel;
    
    //helpers
    public List<Domain> selectedDomains;
    public List<questionWeightArgs> questionWeights;
    public List<addUserToSurveyArgs> participantsRoleList;
    public List<Role> roleList;
    
    // weighing
    public List<WeighDomain> weighList;
    
    // completion
    public List<SurveyUserRole> surList;


    Panel p1;
    Panel p2;
    
    @Override
    public void init() {
        survey = new Survey();
    }

    @Override
    public void setView() {
        newSurveyView = new NewSurveyView(this);
        this.view = newSurveyView;
        this.view.buildLayout();
    }

    @Override
    public void loadModels() {
        domainModel = new DomainModel();
        surveyModel = new SurveyModel();
        userModel = new UserModel();
        roleModel = new RoleModel();
        questionnaireModel = new QuestionnaireModel();
    }

    @Override
    public void setNeededPermissions() {
        neededPermissions.add(Permissions.LOGGEDIN);
    }

    @Override
    public void urlFragmentChanged(int fragmentId, String fragment) {
        
    }
    
    
    /**
    * Saves the data over the surveyModel to the Database
    */
    public void saveToDatabase() {

        //TODO fix
        Questionnaire questionnaire = questionnaireModel.getQuestionnaire(1);
        survey.setQuestionnaire(questionnaire);
        
       //Create Survey
       User u = new User();
       u.setId(userSession.getId());
       survey = surveyModel.createSurvey(survey, u, userSession.getRole());

       //Add Coordinator Relevances
       surveyModel.addQuestionWeights(survey, questionWeights);

       //Add Users to the Survey
       surList = surveyModel.addUsersToSurvey(participantsRoleList, survey);

       newSurveyView.tabComponent4Invitations();
   }
    
   public void sendInvitations() {
       
       newSurveyView.updateTab4();
       
        String coordinator = userSession.getFirstname() + " " + userSession.getLastname(); 
        EmailHelper eh = new EmailHelper();

        for(int i = 0; i < surList.size(); i++) {
            if (surList.get(i).getRole().getId() == Config.EXPERT_ID) {
                eh.sendInvitationMail(surList.get(i), TM.get("email.invitationtext.expert", coordinator, survey.getTitle()));
            }
            else {
            	
                String emailText = newSurveyView.emailEditor.getValue().toString();
                //String emailText = emailEditor.getValue().toString().replace("<br /><br /> <a href=#>http://www.dummylink.com</a> (Placeholder)", "");
                eh.sendInvitationMail(surList.get(i), emailText);
            }
        }
        
        getAppData().getLoaderLib().setUrl("Dashboard/0/");
    }

}
